//###############
//FILE : GoodByeRequest.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.nameserver;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import oop.ex3.protocol.sessions.NameGoodByeSession;

/**
 * This handles the GOODBYE request as specified in the assignment.
 * @author daniel
 */
public class GoodByeRequest extends FileManagerSpecificRequest{
    private NameGoodByeSession _goodByeSession;

    /**
     * Create a new request initialized with a NS database which is passed to the super class.
     * The opening session is a NameGoodByeSession.
     * @param nsDb The nameserver database.
     */
    public GoodByeRequest(NameServerDatabase nsDb) {
        super(nsDb, new NameGoodByeSession());
        _goodByeSession = (NameGoodByeSession)_openingSession;
    }

    @Override
    public boolean serveRequest(DataInputStream inputStream, DataOutputStream outputStream) {
        //System.out.println("serving good bye request");
        //first run the go away session
        if (!_goodByeSession.executeSession(inputStream, outputStream)) {
            return false;
        }

        //if we didn't find the end  then there was an error
        if (!_endSession.executeSession(inputStream, outputStream)) {
            return false;
        }
        
        //remove information about this file manager
        _nameServerDb.removeFileManager(_fileManager);
        
        return true;
    }

    @Override
    public NameServerRequest getCopy() {
        GoodByeRequest copy = new GoodByeRequest(_nameServerDb);
        return copy;
    }

}
